package com.augurit.wf.common.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.wf.common.entity.WfAgencyDetail;

@Repository
public class WfAgencyDetailDao extends BaseDao<WfAgencyDetail, Long> {
	public List<WfAgencyDetail> getBy(Long agencyId){
		if(agencyId != null){
			List<WfAgencyDetail> entities = this.findBy("agencyId", agencyId);
			if(entities != null && entities.size() > 0){
				return entities;
			}
		}
		return null;
	}
	
	public void addAgencyDetail(Long agencyId, Long wfTemplateId){
		if(agencyId != null && wfTemplateId != null){
			WfAgencyDetail entity = new WfAgencyDetail();
			entity.setAgencyId(agencyId);
			entity.setWfTemplateId(wfTemplateId);
			this.save(entity);
		}
		else{
			throw new RuntimeException("Parameters agencyId,  wfDefId, wfDefName are illegal");
		}
		
	}
	
	public void addAgencyDetails(Long agencyId, String[] wfTemplateIds){
		if(agencyId != null && wfTemplateIds != null && wfTemplateIds.length > 0){
			for(String wfTemplateId :wfTemplateIds){
				if(get(agencyId, Long.parseLong(wfTemplateId)) == null)
					this.addAgencyDetail(agencyId, Long.parseLong(wfTemplateId));
			}
		}
	}
	
	public void clearAgentDetail(Long agencyId){
		List<WfAgencyDetail> details = this.getBy(agencyId);
		if(details != null && details.size() > 0){
			this.delete(details);
		}
	}
	
	public WfAgencyDetail get(Long agencyId, Long wfTemplateId){
		WfAgencyDetail entity = null;
		if(agencyId != null && wfTemplateId != null){
			String hql = " from WfAgencyDetail ps where ps.agencyId=? and ps.wfTemplateId=?";
			entity = this.findUnique(hql, agencyId,wfTemplateId);
		}
		return entity;
	}
}